package io.gitee.sfch.chunmi.auth.session;

import io.gitee.sfch.chunmi.auth.ChunmiAuthConf;
import io.gitee.sfch.chunmi.auth.SkipAuthentication;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web权限配置，添加鉴权拦截器，并设置需要放行的路径。不需要拦截的方法可通过注解跳过鉴权
 *
 * @see SkipAuthentication
 */
@Configuration
public class AuthWebMvcConfigurer implements WebMvcConfigurer {

    @Resource
    private ChunmiAuthConf chunmiAuthConf;
    @Resource
    private AuthenticationInterceptor authenticationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 允许通过配置参数来控制是否开启鉴权
        if (!chunmiAuthConf.getEnabled()) {
            return;
        }
        registry.addInterceptor(authenticationInterceptor)
                // 拦截所有请求，包括静态资源文件
                .addPathPatterns("/**")
                // 放行登录页，登陆操作，静态资源
                .excludePathPatterns("/",
                        "/index.html",
                        "/css/**",
                        "/images/**",
                        "/js/**",
                        "/fonts/**");
    }
}
